package com.cskaoyan.gateway.controller.user;

import com.cskaoyan.gateway.bean.register.bo.RegisterBo;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.IMemberService;
import com.mall.user.IRegisterService;
import com.mall.user.IVerifyService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
public class RegisterController {
    @Reference(timeout = 3000, check = false)
    IKaptchaService kaptchaService;

    @Reference
    IRegisterService registerService;

    @Reference
    IVerifyService iVerifyService;

    /**
     * 用户注册
     *      1. 验证验证码
     *      2. 插入member表
     *      3. 插入verify表
     *      4. 发送邮件
     */
    @Anoymous
    @PostMapping("/register")
    public ResponseData register(@RequestBody RegisterBo registerBo, HttpServletRequest httpServletRequest){

        UserRegisterRequest request = new UserRegisterRequest(); // 新建request对象

        String userName = registerBo.getUserName(); // 从bo中取出信息
        String email = registerBo.getEmail();
        String userPwd = registerBo.getUserPwd();
        String captcha = registerBo.getCaptcha();

        //    1. 验证验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        String kaptcha_uuid = CookieUtil.getCookieValue(httpServletRequest, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(kaptcha_uuid);
        kaptchaCodeRequest.setCode(captcha);
        KaptchaCodeResponse kaptchaCodeResponse = kaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        String code = kaptchaCodeResponse.getCode();
        if (!SysRetCodeConstants.SUCCESS.getCode().equals(code)){
            return new ResponseUtil().setErrorMsg(kaptchaCodeResponse.getMsg());
        }
        // 验证成功:
        //    2. 插入member表
        //    3. 插入verify表
        //    4. 发送邮件
        request.setEmail(email); // request
        request.setUserName(userName);
        request.setUserPwd(userPwd);
        UserRegisterResponse registerResponse = registerService.register(request);
        if (SysRetCodeConstants.SUCCESS.getCode().equals(registerResponse.getCode())){
            return new ResponseUtil().setData(null);
        }
        return new ResponseUtil().setErrorMsg(registerResponse.getMsg());
    }

    /**
     * 用户激活
     */
    @Anoymous
    @GetMapping("verify")
    public ResponseData verify(String uid, String username) {
        UserVerifyRequest userVerifyRequest = new UserVerifyRequest();
        userVerifyRequest.setUserName(username);
        userVerifyRequest.setUuid(uid);
        UserVerifyResponse response = iVerifyService.userVerify(userVerifyRequest);

        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(null);
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

}
